package com.yxhl.zoume.core.car.fragment.common;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.expresscar.activity.ExpressCarScheduleActivity;
import com.yxhl.zoume.core.officialcar.presenter.CancelOfficialCarOrderPresenter;
import com.yxhl.zoume.core.officialcar.activity.OfficialCarScheduleActivity;
import com.yxhl.zoume.core.officialcar.view.CancelOfficialCarOrderView;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.TripDetailContainerActivity;
import com.yxhl.zoume.core.tripsmgmt.ui.event.CancelTripOrderEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.car.DaggerOfficialCarComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.domain.interactor.ZMEventSubscriber;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.ZMDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * created by sugarya
 * 取消公务车/专车订单原因反馈
 */
public class CancelCarOrderFeedbackFragment extends BaseFragment implements CancelOfficialCarOrderView {

    private static final String TAG = "CancelCarOrderFeedbackFragment";


    @BindView(R.id.btn_cancel_order_feedback_submit)
    Button mCancelOrderSubmitBtn;

    @BindViews({R.id.acb_cancel_order_feedback_first, R.id.acb_cancel_order_feedback_second, R.id.acb_cancel_order_feedback_third, R.id.acb_cancel_order_feedback_fourth})
    List<AppCompatCheckBox> mCheckBoxes;


    //arguments
    @Inject
    CancelOfficialCarOrderPresenter mCancelOfficialCarOrderPresenter;

    private String mOrderId;

    private boolean isWaiting = false;

    private MaterialDialog mDialog;


    public CancelCarOrderFeedbackFragment() {
        // Required empty public constructor
    }


    public static CancelCarOrderFeedbackFragment newInstance(String orderId) {
        Bundle arguments = new Bundle();
        CancelCarOrderFeedbackFragment fragment = new CancelCarOrderFeedbackFragment();
        arguments.putString(UIConstants.Car.INTENT_KEY_ACTIVITY_CANCEL_OFFICIAL_ORDER_ID, orderId);
        fragment.setArguments(arguments);
        return fragment;
    }


    private void parseArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mOrderId = arguments.getString(UIConstants.Car.INTENT_KEY_ACTIVITY_CANCEL_OFFICIAL_ORDER_ID);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArguments();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        mCancelOfficialCarOrderPresenter.attachView(this);

        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setupToolBar();
        }
    }


    /**
     * 第一个Checkbox 监听
     *
     * @param buttonView
     * @param isChecked
     */
    @OnCheckedChanged(R.id.acb_cancel_order_feedback_first)
    void onFeedBackFirstCheckedChange(CompoundButton buttonView, boolean isChecked) {
        checkButtonEnable(buttonView, isChecked);
    }


    /**
     * 第二个Checkbox 监听
     *
     * @param buttonView
     * @param isChecked
     */
    @OnCheckedChanged(R.id.acb_cancel_order_feedback_second)
    void onFeedBackSecondCheckedChange(CompoundButton buttonView, boolean isChecked) {
        checkButtonEnable(buttonView, isChecked);
    }

    /**
     * 第三个Checkbox 监听
     *
     * @param buttonView
     * @param isChecked
     */
    @OnCheckedChanged(R.id.acb_cancel_order_feedback_third)
    void onFeedBackThirdCheckedChange(CompoundButton buttonView, boolean isChecked) {
        checkButtonEnable(buttonView, isChecked);
    }

    /**
     * 第四个Checkbox 监听
     *
     * @param buttonView
     * @param isChecked
     */
    @OnCheckedChanged(R.id.acb_cancel_order_feedback_fourth)
    void onFeedBackFourthCheckedChange(CompoundButton buttonView, boolean isChecked) {
        checkButtonEnable(buttonView, isChecked);
    }

    /**
     * 取消按钮点击事件
     */
    @OnClick(R.id.btn_cancel_order_feedback_submit)
    public void onClick() {

        String remark = "";
        for (AppCompatCheckBox view : mCheckBoxes) {
            if (view.isChecked()) {
                switch (view.getId()) {
                    case R.id.acb_cancel_order_feedback_first:
                        remark = "未接通司机电话";
                        break;
                    case R.id.acb_cancel_order_feedback_second:
                        remark = "司机车辆故障";
                        break;
                    case R.id.acb_cancel_order_feedback_third:
                        remark = "选错地点";
                        break;
                    case R.id.acb_cancel_order_feedback_fourth:
                        remark = "改变行程";
                        break;

                }
            }
        }

        final String finalRemark = remark;

        ZMDialog dialog = new ZMDialog.Builder(mActivity)
                .setMessage(getString(R.string.special_car_dialog_cancel_schedule_message))
                .setPositiveMessage(getString(R.string.special_car_dialog_confirm))
                .setNegativeMessage(getString(R.string.special_car_dialog_cancel))
                .setPositiveClickEvent(new ZMEventSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        super.onNext(o);
                        if (!isWaiting) {
                            isWaiting = true;
                            mDialog = new MaterialDialog.Builder(mActivity)
                                    .content(R.string.dialog_waiting_processing)
                                    .canceledOnTouchOutside(false)
                                    .progress(true, 0)
                                    .show();

                            mCancelOfficialCarOrderPresenter.cancelOrder(mOrderId, finalRemark);
                        }

                        isWaiting = false;
                    }
                })
                .setNegativeClickEvent(new ZMEventSubscriber())
                .show();
    }


    @Override
    public void renderCancelCarOrderOnSuccess() {
        dismissDialog();
        PromptUtils.showShort(mActivity, "取消成功");

        RxBus.getInstance().send(new CancelTripOrderEvent(mOrderId));

        mActivity.finish();
    }

    @Override
    public void renderCancelCarOrderOnFailure(String msg) {
        dismissDialog();
        PromptUtils.showShort(mActivity, msg);
    }


    /**
     * 设置单选&&检查button是否enable
     *
     * @param buttonView
     * @param isChecked
     */
    private void checkButtonEnable(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            for (AppCompatCheckBox view : mCheckBoxes) {
                if (view != buttonView) {
                    view.setChecked(false);
                }
            }
            mCancelOrderSubmitBtn.setEnabled(true);
        } else {
            mCancelOrderSubmitBtn.setEnabled(false);
        }
    }

    private void dismissDialog() {
        isWaiting = false;
        if (null != mDialog && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        dismissDialog();
        super.onDestroyView();
    }


    @Override
    protected void initializeInjector() {
        DaggerOfficialCarComponent
                .builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(mActivity))
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_cancel_order_feedback;
    }

    @Override
    protected void setupToolBar() {
        if (mActivity instanceof TripDetailContainerActivity) {
            ((TripDetailContainerActivity) mActivity).setToolbarTitle(R.string.fragment_car_cancel_order_season_title);
        }

        if (mActivity instanceof OfficialCarScheduleActivity) {
            ((OfficialCarScheduleActivity) mActivity).setToolbarTitle(R.string.fragment_car_cancel_order_season_title);
        }

        if (mActivity instanceof ExpressCarScheduleActivity) {
            ((ExpressCarScheduleActivity) mActivity).setToolbarTitle(R.string.fragment_car_cancel_order_season_title);
        }
    }


}
